from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from homework.ceshiren.framework.base_page import BasePage
from homework.ceshiren.model.search_advance import SearchAdvance


class SearchPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)

    def search(self, keyword) -> str:
        self.find(By.ID, 'search-term').send_keys(keyword)
        self.click(By.CSS_SELECTOR, '[title="打开高级搜索"]')

        WebDriverWait(self.driver, 10).until(
            expected_conditions.visibility_of_element_located((By.CLASS_NAME, 'topic-title'))
        )
        return self.find(By.CLASS_NAME, 'topic-title').text.lower()

    # def clear_search(self):
    #     self.click(By.ID, 'search-button')
    #     self.click(By.CSS_SELECTOR, '[title="清除搜索内容"]')
    #
    # def filter(self, tags=[], subject_tye=None, range=None):
    #     ...

    def search_advance(self, advance: SearchAdvance) -> []:
        # 搜索框
        if advance.title:
            self.input_title(advance.title)

        # 选择分类
        if advance.category:
            self.select_category(advance.category)

        # 标签
        if advance.tags:
            self.select_tags(advance.tags)

        # 话题
        if advance.status:
            self.select_status(advance.status)

        self.click(By.CSS_SELECTOR, '.btn-primary.search-cta.btn.btn-icon-text.ember-view')
        result_list = self.finds(By.CSS_SELECTOR, '.fps-result')
        title_list = []
        for i in range(len(result_list)):
            title_list.append(result_list[i].find_element(By.CSS_SELECTOR, '.topic-title>span').text)

        return title_list[0:5]

    def input_title(self, keyword: str):
        """
        输入关键字
        :param keyword:
        :return:
        """
        self.find(By.CSS_SELECTOR, '.search-bar>input').send_keys(keyword)

    def select_category(self, category: str):
        """
        选择分类
        :param category:
        :return:
        """
        self.click(By.CSS_SELECTOR, "#search-in-category-header")
        self.find(By.CSS_SELECTOR, '#search-in-category-filter>input').send_keys(category)
        self.click(By.CSS_SELECTOR, '.select-kit-collection>li')
        # return self

    def select_tags(self, tags: str):
        """
        选择标签
        :param tags:
        :return:
        """
        self.click(By.CSS_SELECTOR, '#search-with-tags')
        # self.find(By.CSS_SELECTOR, '#search-with-tags-filter>input').send_keys(tags)
        # self.click(By.CSS_SELECTOR, '.select-kit-collection>li')
        tags_list = self.finds(By.CSS_SELECTOR, '.select-kit-collection>li')
        for item in tags_list:
            if item.get_attribute('data-name') == tags:
                item.click()
                return self

    def select_status(self, status: str):
        """
        选择话题
        :param status:
        :return:
        """
        self.click(By.CSS_SELECTOR, '#search-status-options')
        status_list = self.finds(By.CSS_SELECTOR, '.select-kit-collection>li')
        for item in status_list:
            if item.get_attribute('data-name') == status:
                item.click()
                return self
